/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * JP_ListarAcciones.java
 *
 * Created on Aug 11, 2009, 7:06:25 AM
 */

package gui;

import delegate.GestionDelegate;
import controlacciones.ejb3.entity.TbArea;
import controlacciones.ejb3.entity.TbTipoAccionClasificacionAccionArea;
import controlacciones.ejb3.pojo.TableBeanBinding_DatosAcciones;
import helper.JP_Modelo;
import helper.Utiles_Fecha_Cliente;
import java.sql.SQLException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.text.MaskFormatter;
import helper.Utiles_MensajesDialogo;
import java.util.HashMap;
import java.util.Map;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.view.JasperViewer;

/**
 *
 * 
 */
public class JP_ListarAcciones extends JP_Modelo {

    /** Creates new form JP_ListarAcciones */
    public JP_ListarAcciones() {
        inicializaComponentes();
    }

    private void inicializaComponentes(){
        initComponents();
        this.cargaComboArea();
    }

    private void cargaTabla(){
        TbArea area;
        TbTipoAccionClasificacionAccionArea tipoAccion;
        String fechaIni, fechaFin;
        try {

            area = (TbArea) this.cmbArea.getSelectedItem();
        } catch (Exception e) {
            Utiles_MensajesDialogo.mensajeAlerta("Seleccione el area");
            return;
        }

        try {
            tipoAccion = (TbTipoAccionClasificacionAccionArea) this.cmbTipoAccion.getSelectedItem();
        } catch (Exception e) {
            Utiles_MensajesDialogo.mensajeAlerta("Seleccione el tipo de Accion");
            return;
        }

        fechaIni = this.ftxtFecReg.getText().trim();
        Utiles_Fecha_Cliente fecIni = new Utiles_Fecha_Cliente(fechaIni);
        if( !fecIni.isValida() ){
            Utiles_MensajesDialogo.mensajeAlerta("Ingrese una fecha valida");
            return;
        }

        
        try {
            listaDatos = GestionDelegate.getInstance().getListaDatosAccionesByFecha(tipoAccion, fechaIni);
        } catch (Exception ex) {
            Logger.getLogger(JP_ListarAcciones.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();
        jTableBinding = org.jdesktop.swingbinding.SwingBindings.createJTableBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ, listaDatos, this.tbData);

        org.jdesktop.swingbinding.JTableBinding.ColumnBinding columnBinding0 = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${idAccion}"));
        columnBinding0.setColumnName("Id. Accion ");
        columnBinding0.setColumnClass(String.class);

        org.jdesktop.swingbinding.JTableBinding.ColumnBinding columnBinding1 = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${numInscrip}"));
        columnBinding1.setColumnName("Numero de inscripcion");
        columnBinding1.setColumnClass(String.class);

        org.jdesktop.swingbinding.JTableBinding.ColumnBinding columnBinding2 = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${nombre}"));
        columnBinding2.setColumnName("Nombre");
        columnBinding2.setColumnClass(String.class);

        org.jdesktop.swingbinding.JTableBinding.ColumnBinding columnBinding3 = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${direccion}"));
        columnBinding3.setColumnName("Direccion");
        columnBinding3.setColumnClass(String.class);

        org.jdesktop.swingbinding.JTableBinding.ColumnBinding columnBinding4 = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${descripcionAccion}"));
        columnBinding4.setColumnName("Descripcion Accion");
        columnBinding4.setColumnClass(String.class);

        org.jdesktop.swingbinding.JTableBinding.ColumnBinding columnBinding5 = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${fechaRegistro}"));
        columnBinding5.setColumnName("Fecha Registro");
        columnBinding5.setColumnClass(String.class);

        org.jdesktop.swingbinding.JTableBinding.ColumnBinding columnBinding6 = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${fechaEnvio}"));
        columnBinding6.setColumnName("Fecha Envio");
        columnBinding6.setColumnClass(String.class);

        bindingGroup.addBinding(jTableBinding);
        bindingGroup.bind();
    }



    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

        comboBeanBindingArea1 = new beansbinding.ComboBeanBindingArea();
        comboBeanBindingTbTipoAccionClasificacionAccionArea1 = new beansbinding.ComboBeanBindingTbTipoAccionClasificacionAccionArea();
        jp_Derechohabientes = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tbData = new javax.swing.JTable();
        jp_DocumentoIdentificacion = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        cmbArea = new javax.swing.JComboBox();
        cmbTipoAccion = new javax.swing.JComboBox();
        try{
            mascara = new javax.swing.text.MaskFormatter("##/##/####");
        }catch(Exception e){
            System.out.println("Error en mascara");
        }

        ftxtFecReg = new javax.swing.JFormattedTextField();
        ftxtFecReg = new javax.swing.JFormattedTextField(mascara);

        btnConsultar = new javax.swing.JButton();
        btnReporte = new javax.swing.JButton();
        btnCerrar = new javax.swing.JButton();

        setName("Form"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(controlaccionescomercial.ControlAccionesComercialApp.class).getContext().getResourceMap(JP_ListarAcciones.class);
        jp_Derechohabientes.setBorder(javax.swing.BorderFactory.createTitledBorder(null, resourceMap.getString("jp_Derechohabientes.border.title"), javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, resourceMap.getFont("jp_Derechohabientes.border.titleFont"))); // NOI18N
        jp_Derechohabientes.setName("jp_Derechohabientes"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        tbData.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {},
                {},
                {},
                {}
            },
            new String [] {

            }
        ));
        tbData.setName("tbData"); // NOI18N
        jScrollPane1.setViewportView(tbData);

        javax.swing.GroupLayout jp_DerechohabientesLayout = new javax.swing.GroupLayout(jp_Derechohabientes);
        jp_Derechohabientes.setLayout(jp_DerechohabientesLayout);
        jp_DerechohabientesLayout.setHorizontalGroup(
            jp_DerechohabientesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 750, Short.MAX_VALUE)
        );
        jp_DerechohabientesLayout.setVerticalGroup(
            jp_DerechohabientesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 168, Short.MAX_VALUE)
        );

        jp_DocumentoIdentificacion.setBorder(javax.swing.BorderFactory.createTitledBorder(null, resourceMap.getString("jp_DocumentoIdentificacion.border.title"), javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, resourceMap.getFont("jp_DocumentoIdentificacion.border.titleFont"))); // NOI18N
        jp_DocumentoIdentificacion.setName("jp_DocumentoIdentificacion"); // NOI18N

        jLabel1.setFont(resourceMap.getFont("jLabel1.font")); // NOI18N
        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        jLabel2.setFont(resourceMap.getFont("jLabel2.font")); // NOI18N
        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        jLabel3.setFont(resourceMap.getFont("jLabel3.font")); // NOI18N
        jLabel3.setText(resourceMap.getString("jLabel3.text")); // NOI18N
        jLabel3.setName("jLabel3"); // NOI18N

        cmbArea.setName("cmbArea"); // NOI18N

        org.jdesktop.beansbinding.ELProperty eLProperty = org.jdesktop.beansbinding.ELProperty.create("${lista}");
        org.jdesktop.swingbinding.JComboBoxBinding jComboBoxBinding = org.jdesktop.swingbinding.SwingBindings.createJComboBoxBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, comboBeanBindingArea1, eLProperty, cmbArea);
        bindingGroup.addBinding(jComboBoxBinding);

        cmbArea.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmbAreaActionPerformed(evt);
            }
        });

        cmbTipoAccion.setName("cmbTipoAccion"); // NOI18N

        eLProperty = org.jdesktop.beansbinding.ELProperty.create("${lista}");
        jComboBoxBinding = org.jdesktop.swingbinding.SwingBindings.createJComboBoxBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, comboBeanBindingTbTipoAccionClasificacionAccionArea1, eLProperty, cmbTipoAccion);
        bindingGroup.addBinding(jComboBoxBinding);

        ftxtFecReg.setName("ftxtFecReg"); // NOI18N

        btnConsultar.setFont(resourceMap.getFont("btnConsultar.font")); // NOI18N
        btnConsultar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/org/fife/plaf/Office2003/find.gif"))); // NOI18N
        btnConsultar.setText(resourceMap.getString("btnConsultar.text")); // NOI18N
        btnConsultar.setName("btnConsultar"); // NOI18N
        btnConsultar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnConsultarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jp_DocumentoIdentificacionLayout = new javax.swing.GroupLayout(jp_DocumentoIdentificacion);
        jp_DocumentoIdentificacion.setLayout(jp_DocumentoIdentificacionLayout);
        jp_DocumentoIdentificacionLayout.setHorizontalGroup(
            jp_DocumentoIdentificacionLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jp_DocumentoIdentificacionLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jp_DocumentoIdentificacionLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2)
                    .addComponent(jLabel1)
                    .addComponent(jLabel3))
                .addGap(17, 17, 17)
                .addGroup(jp_DocumentoIdentificacionLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jp_DocumentoIdentificacionLayout.createSequentialGroup()
                        .addComponent(ftxtFecReg, javax.swing.GroupLayout.PREFERRED_SIZE, 108, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(btnConsultar))
                    .addComponent(cmbArea, javax.swing.GroupLayout.PREFERRED_SIZE, 161, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cmbTipoAccion, 0, 625, Short.MAX_VALUE))
                .addContainerGap())
        );
        jp_DocumentoIdentificacionLayout.setVerticalGroup(
            jp_DocumentoIdentificacionLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jp_DocumentoIdentificacionLayout.createSequentialGroup()
                .addGroup(jp_DocumentoIdentificacionLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(cmbArea, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jp_DocumentoIdentificacionLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(cmbTipoAccion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jp_DocumentoIdentificacionLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(ftxtFecReg, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnConsultar)))
        );

        btnReporte.setFont(resourceMap.getFont("btnReporte.font")); // NOI18N
        btnReporte.setIcon(new javax.swing.ImageIcon(getClass().getResource("/org/fife/plaf/Office2003/printpreview.gif"))); // NOI18N
        btnReporte.setText(resourceMap.getString("btnReporte.text")); // NOI18N
        btnReporte.setName("btnReporte"); // NOI18N
        btnReporte.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnReporteActionPerformed(evt);
            }
        });

        btnCerrar.setFont(resourceMap.getFont("btnCerrar.font")); // NOI18N
        btnCerrar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/org/fife/plaf/Office2003/close.gif"))); // NOI18N
        btnCerrar.setText(resourceMap.getString("btnCerrar.text")); // NOI18N
        btnCerrar.setName("btnCerrar"); // NOI18N
        btnCerrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCerrarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(564, Short.MAX_VALUE)
                .addComponent(btnReporte)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnCerrar)
                .addContainerGap())
            .addComponent(jp_DocumentoIdentificacion, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jp_Derechohabientes, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(jp_DocumentoIdentificacion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jp_Derechohabientes, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnCerrar)
                    .addComponent(btnReporte, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        bindingGroup.bind();
    }// </editor-fold>//GEN-END:initComponents

    private void cargaComboArea(){
        List listTbArea = null; // seteamos variable local
        List listaArea = comboBeanBindingArea1.getLista();
        listaArea.clear();
        try {
            listTbArea = GestionDelegate.getInstance().getListaArea();
        } catch (Exception e) {
            System.out.println( "Error en gestionDelegate.getListaArea()" );
            return;
        }
        this.comboBeanBindingArea1.setLista(listTbArea);
        this.bindingGroup.bind();
        this.cmbArea.updateUI();
    }

    private void cargaComboTipoAccion(){
        List listTbTipoAccion = null;
        TbArea seleccionado;
        seleccionado = (TbArea) this.cmbArea.getSelectedItem();
        try {
            listTbTipoAccion = GestionDelegate.getInstance().getTipoAccionByArea(seleccionado.getCodArea());
        } catch (Exception ex) {
            Logger.getLogger(JP_RegistrarAccion1.class.getName()).log(Level.SEVERE, null, ex);
            return;
        }
        this.comboBeanBindingTbTipoAccionClasificacionAccionArea1.setLista(listTbTipoAccion);
        this.bindingGroup.bind();
        this.cmbTipoAccion.setSelectedIndex(-1); //Reseteamos la selección del combo
        this.cmbTipoAccion.updateUI();
    }

    private void btnCerrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCerrarActionPerformed
        this.cerrarPadre();
}//GEN-LAST:event_btnCerrarActionPerformed

    private void cmbAreaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmbAreaActionPerformed
        // TODO add your handling code here:
        if( this.cmbArea.getSelectedItem() != null ){
            this.cargaComboTipoAccion();
        }
    }//GEN-LAST:event_cmbAreaActionPerformed

    private void btnConsultarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnConsultarActionPerformed
        this.cargaTabla();
    }//GEN-LAST:event_btnConsultarActionPerformed

    private void btnReporteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnReporteActionPerformed
        JasperViewer jv = null;
        Map parametros = new HashMap();
        
        TbArea area = null;
        TbTipoAccionClasificacionAccionArea tipoAccion = null;
        String fechaIni = null;
        String fechaFin = null;
        try {
            area = (TbArea) this.cmbArea.getSelectedItem();
        } catch (Exception e) {
            Utiles_MensajesDialogo.mensajeAlerta("Seleccione el area");
            return;
        }

        try {
            tipoAccion = (TbTipoAccionClasificacionAccionArea) this.cmbTipoAccion.getSelectedItem();
        } catch (Exception e) {
            Utiles_MensajesDialogo.mensajeAlerta("Seleccione el tipo de Accion");
            return;
        }

        fechaIni = this.ftxtFecReg.getText().trim();
        Utiles_Fecha_Cliente fecIni = new Utiles_Fecha_Cliente(fechaIni);
        if( !fecIni.isValida() ){
            Utiles_MensajesDialogo.mensajeAlerta("Ingrese una fecha valida");
            return;
        }

        fechaFin = this.ftxtFecReg.getText().trim();
        Utiles_Fecha_Cliente fecFin = new Utiles_Fecha_Cliente(fechaIni);
        if( !fecFin.isValida() ){
            Utiles_MensajesDialogo.mensajeAlerta("Ingrese una fecha valida");
            return;
        }

        parametros.put("COD_AREA", area.getCodArea() );
        parametros.put("ID_TIPO_ACCION", tipoAccion.getTbTipoAccion().getCodTipoAccion());
        parametros.put("FECHA_REGISTRO", fechaIni );
        JasperPrint jp = null;
        try {
            jp = GestionDelegate.getInstance().generarReporte("Reporte_Acciones.jasper", parametros);
        } catch (SQLException ex) {
            Logger.getLogger(JP_ListarAcciones.class.getName()).log(Level.SEVERE, null, ex);
        } catch (JRException ex) {
            Logger.getLogger(JP_ListarAcciones.class.getName()).log(Level.SEVERE, null, ex);
        }
        jv = new JasperViewer(jp, false);
        jv.setExtendedState(javax.swing.JFrame.MAXIMIZED_BOTH);
        jv.setVisible(true);
    }//GEN-LAST:event_btnReporteActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCerrar;
    private javax.swing.JButton btnConsultar;
    private javax.swing.JButton btnReporte;
    private javax.swing.JComboBox cmbArea;
    private javax.swing.JComboBox cmbTipoAccion;
    private beansbinding.ComboBeanBindingArea comboBeanBindingArea1;
    private beansbinding.ComboBeanBindingTbTipoAccionClasificacionAccionArea comboBeanBindingTbTipoAccionClasificacionAccionArea1;
    private javax.swing.JFormattedTextField ftxtFecReg;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JPanel jp_Derechohabientes;
    private javax.swing.JPanel jp_DocumentoIdentificacion;
    private javax.swing.JTable tbData;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration//GEN-END:variables

    private org.jdesktop.swingbinding.JTableBinding jTableBinding;
    private List<TableBeanBinding_DatosAcciones> listaDatos;
    private MaskFormatter mascara;
}
